﻿using System;
using System.Linq;
using System.Net;
using System.Globalization;
using System.Xml.Linq;
using System.Windows.Media.Imaging;

namespace EarthWatchers.SL.TIG
{
    public class TigParser
    {
        public delegate void ChangedEventHandler(object sender, EventArgs e);
        public event ChangedEventHandler TigUserReceived;
        private TigUserinfo tigUser;

        public void GetUserInfo(string name)
        {
            var requestUrl = string.Format(CultureInfo.InvariantCulture, "{0}/?key={1}&format=xml&cmd=fetchuser&query={2}", Constants.TigApiUrl, Constants.TigKey, name);
            var client = new WebClient();
            client.DownloadStringCompleted += ClientDownloadStringCompleted;
            client.DownloadStringAsync(new Uri(requestUrl));
        }

        private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                tigUser = new TigUserinfo();

                var xmlData = e.Result;
                var xmlDoc = XDocument.Parse(xmlData);
                var q = from c in xmlDoc.Descendants("userinfo")
                        select new
                        {
                            Username = c.Element("username").Value,
                            Name = c.Element("name").Value,
                            Joined = c.Element("joined").Value,
                            Avatar_small = c.Element("avatar_small").Value,
                            Avatar_big = c.Element("avatar_big").Value,
                            Country_flag = c.Element("country_flag").Value,
                            Country_name = c.Element("country_name").Value
                        };

                var friends = from c in xmlDoc.Descendants("friends").Elements("username")
                              select new
                              {
                                  Friend = c.Value
                              };

                foreach (var obj in q)
                {
                    tigUser.Username = obj.Username;
                    tigUser.Name = obj.Name;
                    tigUser.Joined = obj.Joined;
                    tigUser.Country = obj.Country_name;
                    tigUser.AvatarSmall = new BitmapImage(new Uri(obj.Avatar_small));
                    tigUser.AvatarBig = new BitmapImage(new Uri(obj.Avatar_big));
                    tigUser.SetFlag(obj.Country_flag);
                }

                foreach (var obj in friends)
                {
                    tigUser.Friends.Add(obj.Friend);
                }

                OnTigUserReceived(new EventArgs());
            }
            catch (Exception)//Awfull LINQ exceptions, probably no data given back (user does not exist)
            {
                tigUser = null;
                OnTigUserReceived(new EventArgs());
            }             
        }

        protected virtual void OnTigUserReceived(EventArgs e)
        {
            if (TigUserReceived != null)
                TigUserReceived(tigUser, e);
        }
    }
}
